/** ###################################################################
**     Filename  : TachIn.C
**     Project   : Motologger_v2
**     Processor : MC9S12XD256BMFU
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 11/14/2009, 7:42 PM
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, spol. s r.o. 1997-2007
**     UNIS, spol. s r.o.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE TachIn */
#include "TachTimer.h"

unsigned short tach_val = 0;
unsigned char  OverflowCount = 0;
unsigned long  OverflowTime = 0;
unsigned short DebounceTime = 0;
unsigned long  TotalTime;
	

TachTimer_TCapturedValue TachInTimerVal;
#define TACHIN_MINTIME 6250 //2.5ms


void TachIn_Update(void){
	TachTimer_GetCaptureValue(&TachInTimerVal);																									 
	TachTimer_Reset();
	
	if(!OverflowCount){
	  DebounceTime = (TachInTimerVal<TACHIN_MINTIME)?TachInTimerVal:0; //debounce false edges
	  OverflowTime = 0;
	}else if(1 == OverflowCount){
		DebounceTime = 0; //for sure no bounce at 26ms
		//OverflowTime = 0xFFFF;
		OverflowCount = 0;		
	}else{
		OverflowTime = 0xFFFF * (OverflowCount-1);
		OverflowCount = 0;
	}
	TotalTime = DebounceTime + TachInTimerVal + OverflowTime; 
  
	
}


void TachIn_Overflow_Update(void){
		if(OverflowCount>20){ //125RPM min
			tach_val = 0;
		}else{
			OverflowCount++;
		}
}

unsigned short TachIn_GetVal(void){
	tach_val = (unsigned short)(150000000ul/TotalTime);  
  return tach_val;																										  
}

/* END TachIn */
																  